<template>
    <div>
        <a-modal v-model:visible="modalVisible" title="扩缩容数据盘" @ok="handleOk" :confirm-loading="confirmLoading">
            <div>
                <a-form :model="form" ref="formRef">
                    <a-form-item label="当前容量" class="mb-16">
                        {{ instance?.Instance?.dataDiskQuota }}GB
                    </a-form-item>
                    <a-form-item label="提示" v-if="instance?.Instance?.availTmpOuotaSize == 0">
                        当前磁盘已到最大值，不可以扩容
                    </a-form-item>
                    <a-form-item label="变更后容量" name="dataDiskSize" :rules="[{ required: true, validator: validDataDiskSize, trigger: 'blur', }]">
                        <a-input style="width:180px;" :placeholder="`可变更范围为50~${maxDataDisk}`" v-model:value="form.dataDiskSize" /><span
                            class="ml-8">GB</span>
                    </a-form-item>
                    <a-form-item label="存储费用">
                        开机时 <span class="col-money">￥{{ formatOpenPrice() }}</span> /小时，关机时<span class="col-money">￥{{ formatClosePrice() }}</span> /小时
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script setup>
import { defineProps, defineEmits, computed, ref } from 'vue';
import { handleInstance } from 'api/modules/api.instance'
import { storeToRefs } from 'pinia'
import userInfoStore from '@/stores/userInfo.js'
const useUserInfoStore = userInfoStore()
const { accountInfo } = storeToRefs(useUserInfoStore);

const props = defineProps({
    modelValue: {
        type: Boolean,
    },
    instance: {
        type: Object,
        default: {}
    },
    setting: {
        type: Array,
        default: []
    }
});

const validDataDiskSize = async (_rule, value) => {
	if (!value) {
		return Promise.reject('请输入变更数据盘大小');
	} else if ( Number(value) < 50 || Number(value) > maxDataDisk.value ) {
		return Promise.reject(`请输入50至${maxDataDisk.value}之间得数值`);
	} else {
		return Promise.resolve();
	}
};

const form = ref({})
const maxDataDisk = ref(50)
maxDataDisk.value = props.instance?.Instance?.availTmpOuotaSize + props.instance?.Instance?.dataDiskQuota

const emit = defineEmits(['update:modelValue', 'handelOpt', 'handelNoBalance']);
const modalVisible = computed({
    get: () => props.modelValue,
    set: v => emit('update:modelValue', v),
});

// 获取价格 
const close_instance_storage_price = ref('')
const open_instance_storage_price = ref('')
const getPrice = () => {
    props.setting.forEach(item => {
        if (item.key == 'close_instance_storage_price') {
            close_instance_storage_price.value = item.value
        } else if (item.key == "open_instance_storage_price") {
            open_instance_storage_price.value = item.value
        }
    })
}

getPrice()

// 确定按钮的回调
const confirmLoading = ref(false)
const formRef = ref()
const handleOk = () => {
    formRef.value.validateFields().then(() => {
        const params = {
            instanceId: props.instance?.Instance?.instanceId,
            method: 'set_tmp_disk_size',
            dataDiskSize: form.value.dataDiskSize
        }
        confirmLoading.value = true
        handleInstance(params).then(res => {
            confirmLoading.value = false
            if (res.code == 0) {
                emit('update:modelValue', false)
                emit('handelOpt', props.instance?.Instance?.instanceId)
            }
        }).catch(err => {
            confirmLoading.value = false
            if (err.code == 10111) {
                emit('update:modelValue', false)
                emit('handelNoBalance', formatOpenPrice())
            }
        })
    })
};

// 开机价格计算
const formatOpenPrice = () => {
    let str = open_instance_storage_price.value
    const discount = accountInfo.value?.userDiscount?.instanceDiscount || 1
    if (form.value?.dataDiskSize) {
        str = (open_instance_storage_price.value * form.value?.dataDiskSize * discount).toFixed(2)
    } else if (props.instance?.Instance?.dataDiskQuota) {
        str = (open_instance_storage_price.value * props.instance?.Instance?.dataDiskQuota * discount).toFixed(2)
    }

    return str
}

// 关机价格计算
const formatClosePrice = () => {
    let str = close_instance_storage_price.value
    const discount = accountInfo.value?.userDiscount?.instanceDiscount || 1
    if (form.value?.dataDiskSize) {
        str = (close_instance_storage_price.value * form.value?.dataDiskSize * discount).toFixed(2)
    } else if (props.instance?.Instance?.dataDiskQuota) {
        str = (close_instance_storage_price.value * props.instance?.Instance?.dataDiskQuota * discount).toFixed(2)
    }

    return str
}

</script>

